package com.dayuanit.dy16.atm.boatm.handler;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.dayuanit.dy16.atm.boatm.service.ISalaryService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class ExcelDataListener extends AnalysisEventListener<ExcelDataListener.SalaryExcelData> {

    private static final int BATCH_COUNT = 12;
    List<SalaryExcelData> list = new ArrayList<SalaryExcelData>(BATCH_COUNT);

    private ISalaryService salaryService;

    private long salaryId;

    public ExcelDataListener(ISalaryService salaryService, long salaryId) {
        this.salaryService = salaryService;
        this.salaryId = salaryId;
    }

    @Override
    public void invoke(SalaryExcelData salaryExcelData, AnalysisContext analysisContext) {
        list.add(salaryExcelData);

        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
        list.clear();
    }

    private void saveData() {
        salaryService.saveSalaryFileData(list, salaryId);
    }

    @Getter
    @Setter
    public static class SalaryExcelData {
        @ExcelProperty("银行卡")
        private String cardNum;

        @ExcelProperty("金额")
        private String amount;
    }
}
